// pages/selectReceived/selectReceived.js

import api from "../../utils/api";
import util from "../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    from: '',
    invID: '',
    radio: '', //未认领
    radio2: '', //已认领
    bankType: '', //到款类型
    bankTypeStr: '',
    date: '',
    searchValue: '',
    activeTab: 0,
    tips: '暂无数据',
    listData: [],
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页显示数据
    loading: true,
    remind: '下拉加载更多',
    nodata: false, //是否没有数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.invMoney = options.money
    this.data.invID = options.id;
    this.data.from = options.from
  },
  getData() {
    console.log(this.data.activeTab)
    if (this.data.activeTab == 0) {
      this.UnClaimed()

    } else if (this.data.activeTab == 1) {
      this.Claimed()
    }
  },
  //未认领数据
  UnClaimed() {
    let that = this;
    let param = {
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize
    };
    api.getUnclaimedList({
      data: param,
      success: function (res) {
        console.log(res.data.data, '789')
        that.analysis(res.data)
      }
    })
  },
  //已认领数据
  Claimed() {
    let that = this;
    let param = {
      invMoney: this.data.invMoney,
      searchStr: that.data.searchValue ? that.data.searchValue : '',
      pageNum: that.data.currentPage,
      pageSize: that.data.pageSize
    };
    api.getDkClaimedList({
      data: param,
      success: function (res) {
        console.log(res.data.data)
        that.analysis(res.data)
      }
    })
  },

  // 清空数据并重新加载数据
  clearData() {
    this.setData({
      listData: [],
      currentPage: 1,
      nodata: false,
      loading: true,
      tips: '暂无数据'
    })
    this.getData()
  },
  // 搜索
  onSearch(event) {
    this.setData({
      searchValue: event.detail.value ? event.detail.value : event.detail,
    })
    this.clearData()
  },
  // 处理数据
  analysis(data) {
    let that = this;
    if (data.code == 200) {
      if (data.data.list.length > 0) {
        console.log(that.data.activeTab)
        for (let i = 0; i < data.data.list.length; i++) {
          let now = that.data.activeTab == 0 ? new Date(data.data.list[i].receiveDate) : new Date(data.data.list[i].claimDate);
          data.data.list[i].date = util.formatTime(now).split(' ')[0];
          data.data.list[i].cusBankAccount = data.data.list[i].cusBankAccount ? data.data.list[i].cusBankAccount.substring(data.data.list[i].cusBankAccount.length - 4, data.data.list[i].cusBankAccount.length) : '';
          data.data.list[i].money = that.data.activeTab == 0 ? util.formatFigure(data.data.list[i].money) : util.formatFigure(data.data.list[i].claimMoney);
          data.data.list[i].claimMoney = that.data.activeTab == 1 ? util.formatFigure(data.data.list[i].claimMoney) : '0'
        }
        that.data.totalPage = data.data.totalPage;
        that.setData({
          listData: that.data.listData.concat(data.data.list),
          loading: false,
          nodata: false,
          remind: data.data.totalPage > data.data.pageNum ? '下拉加载更多' : '已经到底啦～'
        })
      } else {
        that.setData({
          nodata: true
        })
      }

    }
  },
  onChange(event) {
    console.log(event)
    this.setData({
      activeTab: event.detail.index,
      searchValue: '',
    })
    this.clearData();

  },
  clickItem(event) {
    console.log(event, "每一个item")
    this.data.bankType = event.currentTarget.dataset.type; //类型
    this.data.bankTypeStr = event.currentTarget.dataset.typestr; //类型
    this.data.date = event.currentTarget.dataset.date; //时间
    this.data.code = event.currentTarget.dataset.code; //到款项

    if (this.data.activeTab == 0) {
      this.data.receiveId = event.currentTarget.dataset.id;
      console.log(this.data.receiveId, this.data.code, '（未认领)receiveId')
    } else if (this.data.activeTab == 1) {
      this.data.dkClaimId = event.currentTarget.dataset.id;
      console.log(this.data.dkClaimId, this.data.code, '(已认领)dkClaimId')
    }
  },
  onSelectChange(event) {
    if (event.currentTarget.dataset.index == 0) {
      this.setData({
        radio: event.detail,
        radio2: ''
      });
    } else if (event.currentTarget.dataset.index == 1) {
      this.setData({
        radio2: event.detail,
        radio: ''
      });
    }
  },
  onConfirm() {
    wx.setStorageSync('bankType', this.data.bankType)
    wx.setStorageSync('bankTypeStr', this.data.bankTypeStr)
    wx.setStorageSync('date', this.data.date)
    // console.log(this.data.date)
    if (this.data.from == 2) {
      // console.log('2222')
      //调用保存按钮
      api.fpbuchong({
        method: 'post',
        data: {
          id: this.data.invID,
          receiveType: this.data.bankType,
          receiveDate: this.data.date,
          receiveId: this.data.receiveId,
          dkClaimId: this.data.dkClaimId
        },
        success: function (res) {
          // console.log(res.data.data, "fpbuchong")
          if (res.data.code == 200) {
            wx.navigateBack({
              delta: 0,
            });
            // wx.showToast({
            //     title: '保存成功',
            //     icon: 'success'
            // })
          }
        }
      })
    } else {
      // console.log('没有创建发票时候，自己存receTermCode')
      // console.log(this.data.code, 'codeddddddd')
      wx.setStorageSync('receTermCode', this.data.code)
      wx.setStorageSync('receTermDate', this.data.date)
      wx.navigateBack({
        delta: 0,
      });
    }
    //获取到的到款项code
    if (this.data.radio || this.data.radio2) {
      wx.setStorageSync('receTermCode', this.data.code)
      wx.setStorageSync('receTermDate', this.data.date)
      // wx.navigateBack({
      //   delta: 0,
      // });
      // if (this.data.radio) {
      //   console.log(this.data.radio,this.data.code)
      //     wx.setStorageSync('receTerm', this.data.radio)
      //     wx.setStorageSync('receTermCode', this.data.code)

      //     wx.removeStorageSync('receTerm2')
      //     //跳转到选择合同号页面
      //     wx.navigateBack({
      //         delta: 0,
      //     });
      // }
      // if (this.data.radio2) {
      //   console.log(this.data.radio2,this.data.code)

      //     wx.setStorageSync('receTerm2', this.data.radio2)
      //     wx.setStorageSync('receTermCode', this.data.code)

      //     wx.removeStorageSync('receTerm')
      //     // console.log(this.data.radio2)
      //     //跳转到选择合同号页面
      //     wx.navigateBack({
      //         delta: 0,
      //     });
      // }
    } else {
      wx.showModal({
        content: '请选择要添加的到款项',
        showCancel: false,
        confirmColor: '#0041C4'
      })
    }

    //获取到的到款项id
    if (this.data.receiveId) {
      // console.log(this.data.receiveId, this.data.dkClaimId)
      wx.setStorageSync('receiveId', this.data.receiveId)
      wx.removeStorageSync('dkClaimId')
    } else if (this.data.dkClaimId) {
      wx.setStorageSync('dkClaimId', this.data.dkClaimId)
      wx.removeStorageSync('receiveId')
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.clearData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading: true
      })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})